package com.slfx.zookeeper.idgen.demo.rest;

import com.slfx.zookeeper.idgen.demo.sequence.ZkPersistentSequentialDistributedSequence;
import com.slfx.zookeeper.idgen.demo.sequence.ZkVersionDistributedSequence;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @fileName: ZookeeperIdGenController
 * @author: zengqingfa
 * @date: 2023/1/8 10:49
 * @description:
 */
@RestController
@RequestMapping("/zookeeper/id")
public class ZookeeperIdGenController {


    @Resource(name = "zkVersionDistributedSequence")
    private ZkVersionDistributedSequence zkVersionDistributedSequence;

    /**
     * 通过更新数据更新版本号的方式返回id
     *
     * @return
     */
    @GetMapping("/sequenceByVersion")
    public Long sequenceByVersion(String sequenceName) {
        Long sequence = zkVersionDistributedSequence.sequence(sequenceName);
        return sequence;
    }

    @Resource(name = "zkPersistentSequentialDistributedSequence")
    private ZkPersistentSequentialDistributedSequence zkPersistentSequentialDistributedSequence;

    /**
     * 通过创建临时顺序节点的方式返回id
     *
     * @return
     */
    @GetMapping("/persistentSequential")
    public Long persistentSequential(String sequenceName) {
        Long sequence =zkPersistentSequentialDistributedSequence.sequence(sequenceName);
        return sequence;
    }
}
